package com.iraeta.controller.basedata;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.iraeta.controller.BaseController;
import com.iraeta.entity.Duanzao;
import com.iraeta.service.DuanZaoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController(value = "DuanZaoController")
@RequestMapping("duanzao")
public class DuanZaoController extends BaseController {
    @Autowired
    DuanZaoService duanZaoService;

    @RequestMapping("pageduanzao")
    public String pageDuanzao(@RequestBody JSONObject jo){
        String cname = jo.getString("cname");
        this.pageSet(jo);
        List<Duanzao> duanzaoList = duanZaoService.listDuanzao(cname);
        PageInfo<Duanzao> duanzaoPageInfo = this.getPageInfo(duanzaoList);
        return this.success(duanzaoPageInfo);
    }

    @RequestMapping("getduanzao")
    public String getDuanzao(@RequestBody JSONObject paramJb){
        int id = paramJb.getIntValue("id");
        Duanzao DuanZao = duanZaoService.getDuanzao(id);
        return this.success(DuanZao);
    }

    @RequestMapping("addduanzao")
    public String addEditDuanzao(@RequestBody JSONObject paramJb){
        int id = paramJb.getIntValue("id");
        Duanzao DuanZao = id == 0 ? new Duanzao() :  duanZaoService.getDuanzao(id);
        this.mapJsonToObj(paramJb, DuanZao);
        if(id == 0){
            DuanZao.setDeleted(0);
            duanZaoService.addDuanzao(DuanZao);
        }else {
            duanZaoService.updateDuanzao(DuanZao);
        }
        return this.success();
    }

    @RequestMapping("deleteduanzao")
    public String deleteDuanzao(@RequestBody JSONObject paramJb){
        int id = paramJb.getIntValue("id");
        duanZaoService.deleteDuanzao(id);
        return this.success();
    }

    /*获取所有钢类锻造费*/
    @RequestMapping("listallduanzao")
    public String listAllDuanzao(){
        List<Duanzao> duanzaoList = duanZaoService.listAllDuanzao();
        return this.success(duanzaoList);
    }
}
